
#include "precompiled.h"
#include "declarativetask.h"
#include <scripting/declarativeenvironment.h>

#include "declarativeextension.h"


class DeclarativeTaskPrivate{
	public:
	DeclarativeTaskPrivate(DeclarativeExtension* ev):
		ext(ev)
	{}
		~DeclarativeTaskPrivate() {}
		DeclarativeExtension* ext;

		QUrl url;

		void run()
		{
			ext->start();
		}
};


DeclarativeTask::DeclarativeTask(DeclarativeExtension* ext, const QUrl& url) :
	Opl::Core::Task(0), O_PRIVATE_NAME(new DeclarativeTaskPrivate(ext))
{	
	O_D(DeclarativeTask);
	d->url = url;
	setup();
}

DeclarativeTask::~DeclarativeTask()
{
	O_PRIVATE_DELETE;
}

void DeclarativeTask::run()
{    
    O_D(DeclarativeTask);
    d->run();
    while(!quitNow())
    {
        msleep(10);
    }
}

bool DeclarativeTask::isValid()const
{
    O_C(DeclarativeTask);
    return d->ext->valid();
}

void DeclarativeTask::setup()
{
    O_D(DeclarativeTask);
    connect(this, SIGNAL(done()), this, SLOT(cleanup()));
    d->ext->initialize();
}


void DeclarativeTask::cleanup()
{
    O_D(DeclarativeTask);
    d->ext->stop();
}

void DeclarativeTask::changeSource(const QString& script)
{ 
    O_D(DeclarativeTask);
    //d->scriptText = script;
}



